package com.example.ymm.bo;


import com.example.ymm.model.UserLogin;
import com.example.ymm.util.UtilValidate;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * SpringSecurity需要的用户详情
 */
public class YmUserDetails  implements UserDetails,Serializable {
    private UserLogin userLogin;

    public YmUserDetails(UserLogin userLogin) {
        this.userLogin = userLogin;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        //返回当前用户的权限
        return userLogin.getRoleList().stream()
                .filter(permission -> UtilValidate.isNotEmpty(permission))
                .map(permission ->new SimpleGrantedAuthority(permission.trim()))
                .collect(Collectors.toList());
    }

    @Override
    public String getPassword() {
        return userLogin.getPassword();
    }

//    @Override
//    public String getPassword() {
//        return pdUserLogin.getPassword();
//    }

    @Override
    public String getUsername() {
        return userLogin.getUserName();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return false;
    }

//    @Override
//    public boolean isEnabled() {
//        return pdUserLogin.getStatus().equals(1);
//    }


    public UserLogin getUserLogin() {
        return userLogin;
    }

    public void setUserLogin(UserLogin userLogin) {
        this.userLogin = userLogin;
    }
}
